package com.dyg.controller.admin;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.dyg.model.User;
import com.dyg.redis.RedisUtils;
import com.dyg.service.RoleService;
import com.dyg.service.UserService;

@Controller
@RequestMapping("/admin/user")
public class UserController {

	@Autowired
	private UserService userService;
	
	@Autowired
	private RoleService roleService;
	
	@RequestMapping(value = "/list",method = RequestMethod.GET)
	public String userList(Model model){
		model.addAttribute("list",userService.findUserList());
		return "user/list";
	}
	
	@RequestMapping(value = "/add",method = RequestMethod.GET)
	public String add(Model model){
		
		model.addAttribute("roles",roleService.findRoleList());
		return "user/add";
	}
	
	@RequestMapping(value = "/add",method = RequestMethod.POST)
	public String add(
			HttpServletRequest request,
			String username,
			String nickname,
			String password,
			int status
			){
		User user = new User();
		user.setUsername(username);
		user.setNickname(nickname);
		user.setPassword(password);
		user.setStatus(status);
		List<Integer> roleIdList = new ArrayList<Integer>();
        String[] roldIds = request.getParameterValues("roleId");
        for(String roleId:roldIds){
            roleIdList.add(Integer.parseInt(roleId));
        }
		
        userService.add(user,roleIdList);
		return "redirect:list";
	}
	
	
}
